package com.neu.controller;

import com.neu.pojo.CheckIn;
import com.neu.service.CheckInService;
import com.neu.service.PreplotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class CheckInController {
    @Autowired
    private CheckInService checkInService;
    @Autowired
    private PreplotService preplotService;

    /**
     * 获取全部入住信息
     */
    @RequestMapping("getCheckInList")
    @ResponseBody
    public HashMap getCheckInList(){
        List<CheckIn> list= checkInService.getCheckInList();
        HashMap<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("list",list);
        return map;
    }

    /**
     * 根据入住号删除入住信息
     */
    @RequestMapping("delCheckIn")
    @ResponseBody
    public HashMap delCheckIn(int checkInNum){
        int res=checkInService.delCheckIn(checkInNum);
        HashMap<String,Object> map=new HashMap<>();
        if(res>0){
            map.put("code",0);
            map.put("msg","删除成功");
        }
        else {
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }

    /**
     * 根据入住号修改入住信息
     */
    @RequestMapping("updateCheckIn")
    @ResponseBody
    public HashMap updateCheckIn(CheckIn checkIn){
        int res=checkInService.updateCheckIn(checkIn);
        HashMap<String,Object> map=new HashMap<>();
        if(res>0){
            map.put("code",0);
            map.put("msg","修改成功");
        }
        else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return map;
    }

    /**
     * 新增入住信息，同时根据电话号码删除相应的预定信息
     */
    @RequestMapping("addCheckIn")
    @ResponseBody
    public HashMap addCheckIn(CheckIn checkIn){
//        int telephone=checkIn.getTelephone();
        int res=checkInService.addCheckIn(checkIn);
//        int res_2=preplotService.delPreplotByTel(telephone);
        HashMap<String,Object> map=new HashMap<>();
        if(res>0){
            map.put("code",0);
            map.put("msg","增加成功");
        }
        else {
            map.put("code",-1);
            map.put("msg","增加失败");
        }
//        if(res_2>0){
//            map.put("code",0);
//            map.put("msg","删除成功");
//        }
//        else {
//            map.put("code",-1);
//            map.put("msg","删除失败");
//        }
        return map;
    }

    /**
     * 按页获取入住
     */
    @RequestMapping("getCheckInByPage")
    @ResponseBody
    public HashMap getCheckInByPage(int page, int limit){
        List<CheckIn> list=checkInService.getCheckInByPage(page,limit);
        int totalCount=checkInService.getCheckInCount();
        HashMap<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",totalCount);
        map.put("data",list);
        return map;
    }


    @RequestMapping("getCheckInByTel")
    @ResponseBody
    public HashMap getCheckInByTel(long tel){
        CheckIn checkIn=checkInService.getCheckInByTel(tel);
        HashMap<String,Object> map=new HashMap<>();
        if(checkIn!=null){
            map.put("code",0);
            map.put("msg","成功");
            map.put("data",checkIn);
        }
        else {
            map.put("code",-1);
            map.put("msg","请输入正确的手机号");
            map.put("data",checkIn);
        }
        return  map;
    }
}

